package com.moyo.repository;

import com.moyo.model.SysRole;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 角色数据访问（内存实现，后续可替换为JPA/MyBatis）
 */
public class SysRoleRepository {
    private final Map<Long, SysRole> roleMap = new HashMap<>();
    private final AtomicLong idGen = new AtomicLong(1);

    public SysRole save(SysRole role) {
        if (role.getId() == null) {
            role.setId(idGen.getAndIncrement());
        }
        roleMap.put(role.getId(), role);
        return role;
    }

    public Optional<SysRole> findById(Long id) {
        return Optional.ofNullable(roleMap.get(id));
    }

    public Optional<SysRole> findByCode(String code) {
        return roleMap.values().stream()
                .filter(r -> r.getCode().equals(code))
                .findFirst();
    }

    public List<SysRole> findAll() {
        return new ArrayList<>(roleMap.values());
    }

    public void deleteById(Long id) {
        roleMap.remove(id);
    }
} 